/*
 * Copyright (c) 2008-2009 Daniel Freitas
 *
 * This file is part of Army Creator.
 *
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.server.wrapper;

import org.mortbay.jetty.Server;

import com.googlecode.armycreator.server.ArmyCreatorServer;
import com.googlecode.armycreator.server.ServerException;

/**
 * Army Creator server implementation which wraps Jetty application server.
 */
public class ArmyCreatorJettyServer implements ArmyCreatorServer {
    private Server server;

    /**
     * Creates an Army Creator application server.
     * 
     * @param server
     *            The Jetty server instance to use.
     */
    public ArmyCreatorJettyServer(Server server) {
        this.server = server;
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#start()
     */
    @Override
    public void start() {
        if (!isRunning()) {
            try {
                server.start();
            } catch (Exception e) {
                throw new ServerException("Unable to start Army Creator server.", e);
            }
        }
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#join()
     */
    @Override
    public void join() throws InterruptedException {
        if (!isRunning()) {
            throw new IllegalStateException("Unable to join. Army creator server has not been started.");
        }

        server.join();
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#shutdown()
     */
    @Override
    public void shutdown() {
        if (isRunning()) {
            try {
                server.stop();
            } catch (Exception e) {
                throw new ServerException("Unable to stop Army Creator server.", e);
            }
        }
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#isRunning()
     */
    @Override
    public boolean isRunning() {
        return server != null && (server.isRunning());
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#isStopped()
     */
    @Override
    public boolean isStopped() {
        return server != null && server.isStopped();
    }

    /**
     * @see com.googlecode.armycreator.server.ArmyCreatorServer#hasFailed()
     */
    @Override
    public boolean hasFailed() {
        return server != null && server.isFailed();
    }
}
